package com.fadadianqi.logistics.admin.staff.controller;

import com.fadadianqi.logistics.admin.staff.model.Master;
import com.yeqiangwei.commons.util.HttpCookieUtils;
import com.yeqiangwei.commons.util.HttpSessionUtils;
import com.yeqiangwei.commons.util.StringHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author aroot
 */
public class MasterHelper {

    private static final String KET = "m_mm";
    private static final String ID = "m_id";
    private static final String NAME = "m_nm";
    private static final String PASSWORD = "m_pwd";

    public static final String getId(HttpServletRequest request) {
        return HttpCookieUtils.getValue(request, ID, null);
    }

    public static final Master getMaster(HttpServletRequest request) {
        return HttpSessionUtils.getObject(request, KET, null);
    }

    public static final void save(HttpServletRequest request
            , HttpServletResponse response, Master m) {
        if (m == null) {
            return;
        }
        HttpCookieUtils.setValue(response, null, ID, m.getId(), Integer.MAX_VALUE);
        HttpCookieUtils.setValue(response, null, NAME, m.getName(), Integer.MAX_VALUE);
        HttpCookieUtils.setValue(response, null, PASSWORD, m.getPassword(), Integer.MAX_VALUE);
        HttpSessionUtils.setAttribute(request, KET, m);
    }

    public static void del(HttpServletRequest request,
                           HttpServletResponse response) {
        Master m = HttpSessionUtils.getObject(request, "m_m", null);
        if (m == null) {
            return;
        }
        HttpSessionUtils.removeAttribute(request, KET);
        HttpCookieUtils.setValue(response, null, ID, StringHelper.EMPTY, Integer.MAX_VALUE);
        HttpCookieUtils.setValue(response, null, NAME, StringHelper.EMPTY, Integer.MAX_VALUE);
        HttpCookieUtils.setValue(response, null, PASSWORD, StringHelper.EMPTY, Integer.MAX_VALUE);
    }
}
